/*
 * DB2.java
 *
 * Created on November 14, 2007, 2:00 AM
 */

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Minghui
 * @version
 */

public class DB{
    
    private Connection connection=null;
    
    public DB()
     {
        try{
        	//traditional JDBC db connection
        	//not an elegant solution
            Class.forName("com.mysql.jdbc.Driver");
            connection =DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/tc","tc","textbookcrossroads");
            //connection.close();
            }
        catch (ClassNotFoundException e){
            System.err.println("Class not found: "+e);
        }
        catch (Exception e){
            e.printStackTrace();
        }  
    }
    
    public boolean isLogin(ResultSet rs) throws SQLException
    {
        rs.last();
    	return rs.getRow()==1?true:false;
    }
    
    public boolean isAdmin(ResultSet rs) throws SQLException
    {
    	return rs.getInt("role")==1?true:false;
    }
    
    public Connection getDB()
    {
        return this.connection;
    }
    
    public void close()
    {
        try {
            this.connection.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
